<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;

use Log;
use DB;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Validator;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    const EMPTY_CODE = 100, ERROR_CODE = 200, DB_ERROR_CODE = 300, VALIDATE_ERROR = 400;

    /**
     * Controller constructor.
     */
    public function __construct(Request $request)
    {
        $ip = $request->ip();
        $url = $request->path();
        $input = $request->all();
        Log::info($ip . " " . $url, $input);
        DB::connection()->enableQueryLog();
    }

    /**
     * wirte sql logo to log file
     * @return
     */
    protected function sqlLog()
    {
        if (config('app.debug')) {
            Log::debug(__METHOD__, DB::getQueryLog());
        }
    }

    protected function ddSqlLog()
    {
        dd(DB::getQueryLog());
    }

    /**
     * @param $input
     * @param $rules
     * @param $messages
     * @return array
     */
//    public function validate($input, $rules, $messages)
//    {
//        $validator = Validator::make($input, $rules, $messages);
//        if ($validator->fails()) {
//            $errors = $validator->errors()->all();
//            return ["code" => self::VALIDATE_ERROR, "errors" => $errors];
//        }
//        return ["code" => 0];
//    }

    /**
     * response with json
     * @param  string code
     * @param  string msg
     * @param  array data
     * @return Response
     */
    protected function outputJSON($code, $msg = '', $data = [])
    {
        $status = 200;
        if ($code != 0) {
            $status = 500;
        }
        if (empty($data) && $code != 0) {
            return new JsonResponse(compact('code', 'msg'), $status);
        }
        return new JsonResponse(compact('code', 'msg', 'data'), $status);
    }

    /**
     * success response with json
     * @param  array data
     * @return Response
     */
    protected function success($data = [], $msg = "ok")
    {
        return $this->outputJSON(0, $msg, $data);
    }

    /**
     * fail response with json
     * @param  array data
     * @param  string msg
     * @return Response
     */
    protected function fail($code, $msg, $data = "")
    {
        return $this->outputJSON($code, $msg, $data);
    }

    /**
     * JSON 响应
     *
     * @param     $data
     * @param int $code
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function responseJson($data, $code = 200)
    {
        return response()->json($data, $code);
    }

    /**
     * 成功时路由跳转
     *
     * @param $route
     * @param $message
     *
     * @return mixed
     */
    public function successRoutTo($route, $message)
    {
        return redirect()->route($route)->withSuccess($message);
    }

    /**
     * 成功时返回当前页
     *
     * @param $message
     *
     * @return mixed
     */
    public function successBackTo($message)
    {
        return redirect()->back()->withSuccess($message);
    }

    /**
     * 失败时路由跳转
     *
     * @param $route
     * @param $message
     *
     * @return $this
     */
    public function errorRouteTo($route, $message)
    {
        return redirect()->route($route)->withErrors($message);
    }

    /**
     * 失败时返回当前页
     *
     * @param $message
     *
     * @return $this
     */
    public function errorBackTo($message)
    {
        return redirect()->back()->withErrors($message)->withInput();
    }

    /**
     * @api {post} class 调用策略oauth
     * @apiName oAuth
     * @apiVersion 0.0.0
     * @apiGroup A-Define
     * @apiDescription
     * <br>
     * 1.首先获取token  /oauth/token <br>
     *  使用生成的账号获取token <br>
     * access_token 过期时间为15天，refresh_token过期时间为空30天，
     * <br>
     * 参数： <br>
     *  'grant_type' => 'password', //固定为'password'这个字符串<br>
     * 'client_id' => '1',  //暂时不变 <br>
     * 'client_secret' => '2nnogxguoZobxmFUK269GdNc3UMcrFTTbOP3B5hd',//暂时不变<br>
     * 'username' => 'AccountName',//你的账户名<br>
     * 'password' => 'password',//你的账户密码<br>
     *
     * 示例：
     *
     * {
     * "token_type": "Bearer",<br>
     * "expires_in": 1296000,<br>
     * "access_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6IjM2ZDQyYTdmZDk5MTE2MmJjZTk1N2ZhYmEyMDlhNWIxODU2MTY5OWEwNWI4MjZlMTFiNmY0MmQ4Y2EzNWZiMDk5N2ViNGM1MGViZmYwYTMyIn0.eyJhdWQiOiIxIiwianRpIjoiMzZkNDJhN2ZkOTkxMTYyYmNlOTU3ZmFiYTIwOWE1YjE4NTYxNjk5YTA1YjgyNmUxMWI2ZjQyZDhjYTM1ZmIwOTk3ZWI0YzUwZWJmZjBhMzIiLCJpYXQiOjE1MDM4ODQ0ODAsIm5iZiI6MTUwMzg4NDQ4MCwiZXhwIjoxNTA1MTgwNDgwLCJzdWIiOiIxIiwic2NvcGVzIjpbXX0.uAeorEc2taJMk0RKvL1XDWy9_AY0Xzl31YggdIDnZoyD4JN21CpUZVoiLFBs1qyjgR-MUmBXJDT5xkY26-ZMNKbajpiKZoL6sXgk2ghWVpWjQS70-QxtEkBKEN2mHc40ikguSBiCbLrOVd32HQJO9yuJBy-UHuCNkYd0JDiLhRWYKTBMC8G7yjdoOIGU2ZGnV_0OBq6kiz9TnF0pOMfSKmvwTJ86itFGm7n3PXwvxX__8PFU5FaUkKPknikokRY8FU2_o8yFGmgFKklMCwEOeo6RIgKHwBelZCZN6ByKua8lM8PbkXc0yMxIra11hKhSFFzvzJNuw_wbQ-xrLFQL74sZQzzWBCT1LBvWWcfa1vgMKwMV0V3f7PRNuTAFMJLd17-qdGO9q4kSmfQ3oczmTesi4EpWHTbdaRKZbTtJoQ2kGCoQJi33PUtjnIA1PYQAJ3o-cwI_jSAr8Z9Lw1dA0D-Ie2vHbZlEJg4eQRvIsfFJ7iItRk-aWOkE-bObEcBH_cikJvBxxGlVYi3cLLfxqpeX4g25s25iRzIZpX5J1uGb-RPN2RVGMW53-dL1LdLNNxFR322NUNXINSLw-QYQuqUMFLnvdWVIqC9gRROinLaXo7UcQgYf1Ukup9en_QG-XfK4LA7m-5Lr3dW_ZjUpHTBjp1P1SR7JneyyKGAq-qM",<br>
     * "refresh_token": "def502009a0f7f12b696a24cda31bf28468aa5cdfbca778775c296665caf9a8784cb137c856da6f91e4564c679756ad0463d31b5b79433770475492b791cc652e1cfa8b5f8e3881919e4652b21f5dc4faa94b1709a826533c170c866f85f65033d75640801797e41c0236ba21197fd78e9b22fdd786acd84b7ca99c8045275adc028bc757e9c920ddf6b24f607a155bba09409d7af992cf0f703ccf4b956e93fd10bd7993a2f5d0399f6c5b11897e338abf3b8d48b9b0a2b5284897a4533e870c71790260170d518b9e97eac48c6ae1b636a2566d806961d2fede387d8320f02694b8d3cb2f2115b0835afda04b67fe4cc2e752191caa839c1b8cda506479f3e2705e886d3709524aa2d290d49dc2f7aa8cafaf1bdc3e371903df7b7dd8b67e9c17bf4097f3c01582e988ea59d25a8bbd1ef2eb59c8212ae228580e9a673061ac178c437c94a110c5959838db6d670a368c5fafd5f31762de5fff0d2b2c049aa25"<br>
     * }
     *
     * <br>
     * 2.请求中使用token，在head中加入token<br>
     *'headers' => [<br>
     * 'Accept' => 'application/json',<br>
     * 'Authorization' => 'Bearer '.$accessToken,<br>
     * ]
     *
     * <br>
     * 3.若需要刷新token，需要使用refresh_token，如特殊的更新权限的，一般可以不使用
     *<br>
     *  {post}  /oauth/token
     *
     * 参数： <br>
     *  'grant_type' => 'refresh_token', //固定为'refresh_token'这个字符串<br>
     * 'client_id' => '1',  //暂时不变 <br>
     * 'client_secret' => '6hg6XB5DZpM84lMQWVOBBjeYHAekbKhO8gT9AXCj',//暂时不变<br>
     * 'refresh_token' => 'the-refresh-token',//之前保存的refresh_token<br>
     *
     */


}
